//index.js
//获取应用实例
const app = getApp()
var utilMd5 = require('../nextindex/md5.js');
Page({
  data: {
    userInfo: null,
    hasUserInfo: false,
    loginInfo: {
      title: '微信授权',
      content: '获得您的公开信息(昵称、头像等)',
      logName: '博晟安全',
      logImage: '/images/wx_login.png',
    },
    hidden:true,
    condition:true,
    setDataNum:0,
    companyName:""
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../detailtest/index'
    })
  },
  onLoad: function () {
    
  //  wx.downloadFile({
      // 示例 url，并非真实存在
        
    var that = this;
    wx.getStorage({
      key: 'user',
      success(res) {
          that.setData({
            companyName: JSON.parse(res.data).user.companyName
          })
      }
    })
    this.looknumber();
    // var that = this;
    // wx.setStorage({
    //   key: "user",
    //   data: "value"
    // })
    // wx.getStorage({
    //   key: 'user',
    //   success(res) {
    //     that.setData({
    //       condition: false
    //     })
    //   }
    // })
  },
   /* 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '应急预案智能管理小助手',
      path: '/pages/nextindex/index'
    }
  },
  onShow() {
    
    
  },
  onConfirm(e) { // 点击允许
    
    let dialogComponent = this.selectComponent('.wxc-dialog');
    dialogComponent && dialogComponent.hide();
    console.log(e.detail.detail.rawData)
    app.globalData.userInfo = JSON.parse(e.detail.detail.rawData);
    this.userInfo = app.globalData.userInfo;
    if (!this.userInfo) {
      return;
    }
    // this.globalData.userInfo = userInfo
    wx.setStorageSync('userInfo', app.globalData.userInfo)
  },
  onCancel() { // 点击拒绝
    let dialogComponent = this.selectComponent('.wxc-dialog');
    dialogComponent && dialogComponent.hide();
  },
  confirm(){
    this.setData({
      hidden:true
    })
  },
  cancel(){
    this.setData({
      hidden: true,
      condition:true
    })
  },
  dofirstchange(){
    wx.navigateTo({
      url: '../myCoupon/myCoupon'
    })
  },
  tochangedemo(){
    wx.navigateTo({
      url: '../searchrecord/index'
    })
  },
  dotwochange() {
    var companyId = "";
    // wx.navigateTo({
    //   url: '../Compilation/index'
    // })
    wx.getStorage({
      key: 'user',
      success(res) {
        console.log(res)
        companyId = JSON.parse(res.data).user.companyId;
        wx.request({
          url: app.globalData.Url+"/emergency/v1.0/summary/plans", //仅为示例，并非真实的接口地址
          data: {
            companyId: companyId,
          },
          method: "post",
          header: {
            'content-type': 'application/json', // 默认值,
            'Token': JSON.parse(res.data).token,
            'userId': JSON.parse(res.data).user.id
          },
          success(res) {
            if (!res.data.meta.success){
               wx.showToast({
                 title: '暂无汇编',
                 icon: 'none',
                 duration: 1000
               })
               return false;
             }
              wx.setStorage({
                key: "urlnew",
                data: res.data.data[0].viewUrl
              })

            let baseUrl = res.data.data[0].viewUrl;
            var urlpath = baseUrl.indexOf("?") + 1;
            urlpath = baseUrl.substring(urlpath);
            var arrapi = urlpath.split("&");
            let uesrMessage = {}
            arrapi.forEach(
              (item) => {
                var brrapi = [];
                brrapi = item.split("=");
                uesrMessage[brrapi[0]] = brrapi[1];
              }
            )
            let id = uesrMessage.srcFid;
            let expires = Date.parse(new Date());
            let a = "FastDFS1234Bossien" + id + expires
            let token = utilMd5.hexMD5(a)
            token = token.toUpperCase()
            wx.getStorage({
              key: 'user',
              success(res) {
                wx.request({
                  url: app.globalData.Urlnew+"/queryPublishFileInfo.do?appId=001&srcFid=" + id + "&expires=" + expires + "&token=" + token, //仅为示例，并非真实的接口地址
                  data: {},
                  method: "GET",
                  header: {
                    'content-type': 'application/json', // 默认值
                    'Token': JSON.parse(res.data).token,
                    'userId': JSON.parse(res.data).user.id
                  },
                  success(res) {
                    let status = res.data;
                    if (!status.success) {
                      wx.showToast({
                        title: '文件正在转码中，请稍后再查看',
                        icon: 'none',
                        duration: 1000
                      })
                      return false;
                    }
                    if (status.dataObj.srcFileStatusName == "12") {
                      wx.navigateTo({
                        url: '../Compilation/index'
                      })
                    } else {
                      wx.showToast({
                        title: '文件正在转码中，请稍后再查看',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            })
              // wx.downloadFile({
              //   // 示例 url，并非真实存在
              //   url:res.data.data[0].viewUrl,
              //   success: function (res) {
              //     const filePath = res.tempFilePath
              //     debugger;
              //     wx.openDocument({
              //       filePath: filePath,
              //       fileType:"pdf",
              //       success: function (res) {
              //         console.log('打开文档成功')
              //       }
              //     })
              //   }
              // })
          }
        })
        // console.log(res.data)
      }
    })
  },
  looknumber(){
    var that = this;
    let params = {

    };
    wx.getStorage({
      key: 'user',
      success(res) {
        var conpanyId = JSON.parse(res.data).user.companyId;
        params ={
          pageIndex: that.data.pageIndex4,
          pageSize: 20,
          companyId: JSON.parse(res.data).user.companyId,
        }
        wx.request({
          url: app.globalData.Url+"/emergency/v1.0/plan/list", //仅为示例，并非真实的接口地址
          data: params,
          method: "post",
          header: {
            'content-type': 'application/json', // 默认值
            'Token': JSON.parse(res.data).token,
            'userId': JSON.parse(res.data).user.id
          },
          success(res) {
            if(res.data.meta.success){
              var listnew = res.data.data.total;
              that.setData({
                setDataNum: listnew
              })
            }
            }
        })

      }
    })
  }

})
